//
//  BaseTableViewViewController.m
//  OCUI
//
//  Created by bus365-03 on 15/9/29.
//
//

#import "BaseTableViewViewController.h"
#import "NSContact.h"
#import "NSContactGroup.h"
@interface BaseTableViewViewController ()
{
}
@end

@implementation BaseTableViewViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [_tableView setEditing:YES];
    // Do any additional setup after loading the view.
}
- (void)initTableView {
    
   
    _contacts=[[NSMutableArray alloc]init];
    NSContact *contact1=[[NSContact alloc]init];
    contact1.userName=@"AAAA";
    contact1.userAddress=@"从回龙观－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营";
    contact1.isShow=NO;
    
    NSContactGroup *group1=[NSContactGroup initWithName:@"A" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1, nil]];
    [_contacts addObject:group1];
  
    
    NSContact *contact2=[[NSContact alloc]init];
    contact2.userName=@"BBBB";
    contact2.userAddress=@"从回龙观－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营－>霍营";
    contact2.isShow=NO;
   
  
    NSContactGroup *group2=[NSContactGroup initWithName:@"B" andDetail:@"With names beginning with B" andContacts:[NSMutableArray arrayWithObjects:contact2, nil]];
    [_contacts addObject:group2];
    
    
    _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    //设置数据源，注意必须实现对应的UITableViewDataSource协议
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"计算分组数");
    return _contacts.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    NSLog(@"生成组（组%li）名称",(long)section);
    NSContactGroup *group=_contacts[section];
    return group.name;
  
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    NSLog(@"计算每组(组%li)行数",(long)section);
    NSContactGroup *group1=_contacts[section];
    return group1.contacts.count;
  
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NSLog(@"生成尾部（组%li）详情",(long)section);
    NSContactGroup *group=_contacts[section];
    return group.detail;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *TableSampleIdentifier = @"CellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    NSContactGroup *group=_contacts[indexPath.section];
    NSContact *contact=group.contacts[indexPath.row];
    
    UIImage *image = [UIImage imageNamed:@"qq"];
    cell.imageView.image = image;
    UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
    cell.imageView.highlightedImage = highLighedImage;
    cell.textLabel.text=contact.userName;
    cell.detailTextLabel.text=contact.userAddress;
    cell.detailTextLabel.lineBreakMode=NSLineBreakByWordWrapping;
    cell.detailTextLabel.numberOfLines=0;
    cell.detailTextLabel.hidden=!contact.isShow;
    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSContact *contact=_contacts[indexPath.row];
//    UITableViewCell *cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
//    if(contact.isShow==NO)
//    {
//        CGFloat h1=cell.detailTextLabel.frame.size.height;
//        CGFloat h2=cell.frame.size.height;
//        return h2-h1;
//    }
//    return cell.frame.size.height+10;
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
//    NSContact *contact=_contacts[indexPath.row];
//    contact.isShow=!contact.isShow;
//    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSContactGroup *group=_contacts[indexPath.section];
        
        NSContact *contact=group.contacts[indexPath.row];

        [group.contacts removeObjectAtIndex:indexPath.row];

       
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
    }else if(editingStyle == UITableViewCellEditingStyleInsert ){
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
        NSContactGroup *group=_contacts[indexPath.section];
        
         NSContact *contact=group.contacts[indexPath.row];
        [group.contacts insertObject:contact atIndex:indexPath.row];
        //[_contacts insertObject:contact atIndex:indexPath.row];
        
        
        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row]%2==0)
    {
        return UITableViewCellEditingStyleInsert;
    }
    else
    {
        return UITableViewCellEditingStyleDelete;

    }
    
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSUInteger fromRow = [sourceIndexPath row];
    NSUInteger toRow = [destinationIndexPath row];
    
    id object = [_contacts objectAtIndex:fromRow];
    [_contacts removeObjectAtIndex:fromRow];
    [_contacts insertObject:object atIndex:toRow];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
